package com.example.demo.service.impl;

import com.example.demo.dao.ProductDao;
import com.example.demo.entity.response.Response;
import com.example.demo.entity.sqlbean.Product;
import com.example.demo.entity.sqlbean.ProjectProduct;
import com.example.demo.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {


    @Autowired
    ProductDao productDao;


    /**
     * 查询project所属product Message
     * @param projectId
     * @return
     */
    @Override
    public Response selectProductById(Long projectId) {
        Response response;
        List<ProjectProduct> projectProductList = productDao.selectProductIdList(projectId);
        if (projectProductList.size() == 0){
            response = Response.error(3001,"无产品数据");
        }
        else
        {
            for (int i = 0; i < projectProductList.size(); i++) {
                ProjectProduct projectProducts = productDao.selectProductList(projectProductList.get(i).getProductId());
                projectProductList.get(i).setPicture(projectProducts.getPicture());
                projectProductList.get(i).setName(projectProducts.getName());
                projectProductList.get(i).setType(projectProducts.getType());
                projectProductList.get(i).setKind(projectProducts.getKind());
            }
            response = Response.success();
            response = Response.success(projectProductList);

        }

        return response;
    }

    @Override
    public Response selectProductByProduct(Product product) {
        Response response;
        Product products = productDao.selectProductDetails(product.getProjectId(),product.getProductId());
        if (products == null){
            response = Response.error(3002,"产品详情查询失败");
        }
        else
        {
            response = Response.success();
            response = Response.success(products);
        }
        return response;
    }


}
